﻿@{
    ViewBag.Title = "Quản lý tin";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";
}
@section _Head{
    <script type="text/javascript">
        $("#nav-left").accordion("activate", 1);
    </script>
 }
<script type="text/javascript">

    var ListViewIndex = 0;
    var ListViewRecord = 10;

    $(document).ready(function () {

        $(".listview_cal_prev_link").click(function () {
            if (ListViewIndex > 0) {
                ListViewIndex = parseInt(ListViewIndex) - 1;
                LoadNews();
            }
        });

        $(".listview_cal_next_link").click(function () {
            var max = parseInt($("#hidden_listview_total_page").val());
            if (max - (parseInt(ListViewIndex) + 1) > 0) {
                ListViewIndex = parseInt(ListViewIndex) + 1;
                LoadNews();
            }
        });

        $(".list_view_record").change(function () {
            ListViewRecord = $(this).val();
            ListViewIndex = 0;
            LoadNews();

        });

        LoadNews();       
    });

  
    function HandlerEventForLink() {       
        $('.delete').click(function () {
            var isDelete = confirm("Bạn chắc chắn muốn xóa?");
            var id = $(this).attr("rel");
            if (isDelete) {
                DeleteNews(id);
            }
        });
    }
   
    function LoadNews() {

        jQuery("#list_container").showLoading();
        var languageId = "";
        $.ajax({
            type: "POST",
            url: "/Admin/News/GetDisplayForAdmin",
            data: ({ languageId: languageId, maxRecord: ListViewRecord, currentPage: ListViewIndex }),
            dataType: "json",
            error: function (xhr, status, error) {
            },
            success: function (d) {
                if (d != null) {

                    if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                        if (d.ErrorFriendlyMessage != null) {
                            ShowErrorMessage(d.ErrorFriendlyMessage);
                        }
                        return;
                    }

                    CreateLayout(d.Data);

                    if (d.TotalPage != null) {

                        $("#hidden_listview_total_page").val(d.TotalPage);
                    }

                    if (d.PadingPage != null) {
                        $(".listview_cal_pading_page").html(d.PadingPage);
                        HandlerPadingPage();
                    }
                }
            },
            complete: function () {
                jQuery("#list_container").hideLoading();
            },
            cache: false
        });    //end ajax
    }


    function DeleteNews(id) {

        $.ajax({
            type: "POST",
            url: "/Admin/News/DeleteForAdmin",
            data: ({ id: id }),
            dataType: "json",
            error: function (xhr, status, error) {
            },
            success: function (d) {
                if (d != null) {

                    if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                        if (d.ErrorFriendlyMessage != null) {
                            ShowErrorMessage(d.ErrorFriendlyMessage);
                        }
                        return;
                    }
                }
            },
            complete: function () {
                LoadNews();
            },
            cache: false
        });        //end ajax
    }

   
    function CreateLayout(data) {
        if (data != null) {
            $('#sealight_list').setTemplate($('#list_template').html());
            $('#sealight_list').processTemplate(data);

            HandlerEventForLink();
        }
    }

    function HandlerPadingPage() {

        $("a.listview-page-number").click(function () {
            ListViewIndex = parseInt($(this).attr("rel"));
            LoadNews();
        });
    }

    function GetImage(imageName) {
        var imageSource;
        if (imageName == null || imageName == undefined || imageName == '') {
            imageSource = "~/Content/images/no-photo.png";
            return GetImageUrl(imageSource, 150, 150, true, 100);
        }
        else {
            imageSource = "~/Content/images/news/" + imageName;

            return GetImageUrl(imageSource, 150, 150, true, 100);
        }
    }

</script>
<h2>Quản lý tin tức</h2>
<div class="options">
    <input type="button" onclick="location.href='/admin/news/AddUpdate'" value="Thêm mới tin"
        id="btnAddNew" class="adminButtonBlue" title="Thêm mới tin"/>
</div>
<div id="manage_container">    
    <div id="list_container">
        <div class="listview_nav">
            <div class="listview_cal_button">
                <div class="listview_cal_prev_button" style="float: left;">
                    <a href="javascript:void(0)" class="listview_cal_prev_link">Trước</a>&nbsp;</div>
                <div class="listview_cal_pading_page" style="float: left;">
                </div>
                <div class="listview_cal_next_button" style="float: left;">
                    &nbsp; <a href="javascript:void(0)" class="listview_cal_next_link">Sau</a>
                </div>                
            </div>
            <div style="float: right;">
                <div class="listview_select_record" style="float: left;">
                    &nbsp;records/Trang:
                    <select class="list_view_record" style="width: 80px;">
                        <option value="5">5</option>
                        <option value="10" selected>10</option>
                        <option value="15">15</option>
                        <option value="20">20</option>
                        <option value="25">25</option>
                        <option value="30">30</option>
                    </select>
                </div>
            </div>
        </div>
        <input type="hidden" id="hidden_listview_total_page" name="hidden_listview_total_page" />
        <input type="hidden" id="hidden_listview_interval_year" name="hidden_listview_interval_year" />
        <br clear="all" />
        <div id="sealight_list">
        </div>
        <br clear="all"/>
        <div class="listview_nav">
            <div class="listview_cal_button">
                <div class="listview_cal_prev_button" style="float: left;">
                    <a href="javascript:void(0)" class="listview_cal_prev_link">Trước</a>&nbsp;</div>
                <div class="listview_cal_pading_page" style="float: left;">
                </div>
                <div class="listview_cal_next_button" style="float: left;">
                    &nbsp; <a href="javascript:void(0)" class="listview_cal_next_link">Sau</a>
                </div>                
            </div>
            <div style="float: right;">
                <div class="listview_select_record" style="float: left;">
                    &nbsp;records/Trang:
                    <select class="list_view_record" style="width: 80px;">
                        <option value="5">5</option>
                        <option value="10" selected>10</option>
                        <option value="15">15</option>
                        <option value="20">20</option>
                        <option value="25">25</option>
                        <option value="30">30</option>
                    </select>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="list_template">
    <table width="700" cellpadding="4" cellspacing="0" border="0" id="sealight_list_items" style="border:1px solid #ccc;" class="tablesorter">	
    <thead>
    <tr>   	
					<th width="150" align="left" bgcolor="#333333">Tiêu đề</th>
					<th width="300" align="justify" bgcolor="#333333">Mô tả</th>
					<th width="150" align="left" bgcolor="#333333">Hình ảnh</th>
                    <th width="50" align="left" bgcolor="#333333"></th>
                    <th width="50" align="left" bgcolor="#333333"></th>
    </tr>
    </thead>
    <tbody>
    {#foreach $T as record}
        <tr class="{#cycle values=['odd','even']}">        	
					<td> {$T.record.title}</td>
					<td> {HtmlDecode($T.record.description)}</td>				
					<td> <img src="{GetImage($T.record.image_url)}" /></td>
                    <td><a href="/admin/news/addupdate/{$T.record.id}" class="edit" style="color:#06C;">Sửa</a></td>         
                    <td><a href="javascript:void(0)" class="delete" rel="{$T.record.id}" style="color:#06C;">Xóa</a></td>                      
        </tr>
    {#/for}
    </tbody>

</table>
</script>